package com.ltmonitor.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.ltmonitor.dao.IBaseIbatisDao;

@Repository("baseIbatisDao")
public class BaseIbatisDao  extends SqlSessionDaoSupport implements IBaseIbatisDao{

    public List<Map<String, Object>> queryByPagination(String queryId, Map params ,PageBounds pageBounds)
    {
    	List<Map<String, Object>> result = null;
    	result = this.getSqlSession().selectList(queryId, params, pageBounds);
    	return result;
    }
    

    public List<Map<String, Object>> query(String queryId, Map params)
    {
    	List<Map<String, Object>> result = null;
    	result = this.getSqlSession().selectList(queryId, params);
    	return result;
    }
    

	@Override
	public void update(final String statementName, Object paramMap)
	{
		getSqlSession().update(statementName, paramMap);
	}
	@Override
	public void insert(final String statementName, Object paramMap)
	{
		getSqlSession().insert(statementName, paramMap);
	}
	
	@Override
	public void createGpsInfoTableIfNotExist(String tableName)
	{
		if(checkTableExist(tableName) == false)
		{
			HashMap paramMap = new HashMap();
			paramMap.put("tableName", tableName);
			//创建历史轨迹表
			update("jt808server.createTableForGpsInfo", paramMap);
			//创建索引
			//update("createIndexForGpsInfoTable", paramMap);
		}
	}
	
	@Override
	public void createNewAlarmTableIfNotExist(String tableName)
	{
		if(checkTableExist(tableName) == false)
		{
			HashMap paramMap = new HashMap();
			paramMap.put("tableName", tableName);
			//创建历史轨迹表
			update("jt808server.createTableForNewAlarm", paramMap);
		}
	}
	
	/**
	 * 检测库表是否存在
	 * @param tableName
	 * @return
	 */
	@Override
	public boolean checkTableExist(String tableName)
	{
		Map params = new HashMap();
		params.put("tableName", tableName);
		List result = getSqlSession().selectList("jt808server.checkTableExist", params);
		return result.size() > 0;
	}
	
	/**
	 * 批量插入数据库
	 * @param insertStatementId
	 * @param list
	 */
	@Override
	@Transactional
	public void batchInsert(final String insertStatementId, final List list) {
		for(Object obj : list)
		{
			this.getSqlSession().insert(insertStatementId,obj);
		}
		
	}
	/**
	 * 批量更新数据库
	 * @param statementName
	 * @param list
	 */
	@Override
	@Transactional
	public void batchUpdate(final String statementName, final List list) {
		try {
			for(Object obj : list)
			{
				this.getSqlSession().update(statementName,obj);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

}
